package std

import (
	"database/sql"
	"fmt"

	"rdbmsx/config"

	"github.com/ClickHouse/clickhouse-go/v2"
)

func ConnectAuth() error {
	houseConfig := config.ClickHouseConfig.Option()
	conn := clickhouse.OpenDB(&houseConfig)

	return conn.Ping()
}

func ConnectDSNAuth() error {
	conn, err := sql.Open("clickhouse", fmt.Sprintf("http://%s?username=%s&password=%s", config.ClickHouseHost, config.ClickHouseUser, config.ClickHousePassword))
	if err != nil {
		return err
	}

	if err = conn.Ping(); err != nil {
		return err
	}

	conn, err = sql.Open("clickhouse", fmt.Sprintf("http://%s:%s@%s", config.ClickHouseUser, config.ClickHousePassword, config.ClickHouseHost))
	if err != nil {
		return err
	}

	return conn.Ping()
}
